<?PHP
require_once WACT_ROOT . 'view/view.inc.php';
require_once APP_ROOT . "model/common/user.php";
require_once APP_ROOT . "model/common/logobanners.php";
require_once APP_ROOT . "model/common/categories.php";
require_once APP_ROOT . "model/common/leftmenu.php";
require_once APP_ROOT . "model/common/page.php";
require_once APP_ROOT . "model/common/session.php";
require_once APP_ROOT . "model/common/site.php";
require_once APP_ROOT . "model/common/rotation.php";
require_once APP_ROOT . "model/common/parameters.php";
require_once 'include/inc.config.php';

class LayoutViewHelper {
   var $IsAdminView;
   var $Template;
   var $view;

   function LayoutViewHelper(&$myview) {
        $this->view =& $myview;
        $this->Template =& $myview->Template;
   }

   function prepare() {

        //$docbase = HOST.'';
        //print $docbase;
    	//$this->Template->set('docbase',$docbase);
        $path = $_SERVER["REQUEST_URI"];
    	if (strstr($path,"adm")) $this->IsAdminView = true;
    	else $this->IsAdminView = false;
    	//print $path;

        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());


    	$this->Template->set('app_name',APP_NAME);
    	$this->Template->set('skip_left_menu',true);

        if(!Session::isLogin()) {
    		$this->Template->set('login',' ');
            //$responseModel->set('login',' ');
    	}
        if (!$this->IsAdminView) {
        	$topbot_leftmenu =& CategoriesModel::getLeftMenu();
        	$this->Template->setChildDataSource('topleftmenu', $topbot_leftmenu);
        	$this->Template->setChildDataSource('bottomleftmenu', $topbot_leftmenu);

        	$topbot_rightmenu=& CategoriesModel::getRightMenu();
        	$this->Template->setChildDataSource('toprightmenu', $topbot_rightmenu);
       		$this->Template->setChildDataSource('bottomrightmenu', $topbot_rightmenu);

        	$topdown_menu=& CategoriesModel::getTopDownMenuList();
        	$this->Template->setChildDataSource('topdownmenu', $topdown_menu);

        	//left menu
        	$leftmenu=& LeftMenuModel::getLeftMenuList(1);
        	$this->Template->setChildDataSource('leftmenu',$leftmenu);

        	//current date
        	$date=LeftMenuModel::getCurDate();
        	$this->Template->set('curdate', $date);

        	//logo & banners
        	$logo=&LogoBannersModel::getLogo();
        	$this->Template->set('logo', $logo);

            // rotating banner
        	$banner =& Rotation::getRandomRecord();
        	//print_r($banner);
        	if ($banner) {
        	    $source = $banner['source'];
        	    $this->Template->set('rotation_show', 1);
        	    if (!$source || $source == 0) {
            		$this->Template->set('rotation_url', $banner['url']);
            		$this->Template->set('rotation_image', $banner['image']);
            		$this->Template->set('rotation_alt', $banner['alt']);
            		//print 'Image';
            	}   else {
                   	$this->Template->set('rotation_script', $banner['script']);
                	//$this->Template->set('rotation_script', 'test script');
                	//print 'script';
            	}
        	}

        	//top left hot links
        	$hotlinksItem =& $this->Template->findChild('hotlinks');
        	if ($hotlinksItem) {
        		$hotlinks =& LeftMenuModel::getLeftMenuList(4);
        		//print_r($hotlinks);
        		$this->Template->setChildDataSource('hotlinks',$hotlinks);
        	}

        	//$banner=&LogoBannersModel::getBanner('TOP');
        	//$this->Template->set($banner[0], $banner[1]);
        	//$this->Template->set('TOPbanner_link', $banner[2]);
        	//$this->Template->set('TOPbanner_alt', $banner[3]);

        	//$banner=&LogoBannersModel::getBanner('LEFT');
        	//$this->Template->set($banner[0], $banner[1]);
        	//$this->Template->set('LEFTbanner_link', $banner[2]);
        	//$this->Template->set('LEFTbanner_alt', $banner[3]);
        }

        //welcome, user
        if(isset($_SESSION['USER'])) {
        	$user=$_SESSION['USER'];
            $is_admin = User::isExistingUserAdmin($user);
        	$is_author = User::isExistingUserArticleWriter($user);
        	if ($is_author) $this->Template->set('IsAuthor',1);
        	if ($is_admin) $this->Template->set('IsAdmin',1);
            $this->Template->set('username',''.$user['login']);
        	$this->Template->set('welcome_user','Welcome, '.$user['login']);
        } //else $this->Template->set('username','guest');

        // populate static pages links
        $member_faq_url = Page::getPublicURLByName('FAQ');
        //if (!$member_faq_url) $member_faq_url = Page::getPublicURLByName('FAQ');
        $this->Template->set('member_faq_url', $member_faq_url);

        $newspaper_stand_list = Page::getPublicURLByName('Newspaper Stand List');
        $this->Template->set('newspaper_stand_list', $newspaper_stand_list);

        $press_releases = Page::getPublicURLByName('Press Releases');
        $this->Template->set('press_releases', $press_releases);

        $about_us_url = Page::getPublicURLByName('About Us');
        $this->Template->set('about_us_url', $about_us_url);
        $advertise_with_us_url = Page::getPublicURLByName('Advertise with Us');
        $this->Template->set('advertise_with_us_url', $advertise_with_us_url);
        $faq_url = Page::getPublicURLByName('FAQ');
        $this->Template->set('faq_url', $faq_url);
        $contact_us_url = Page::getPublicURLByName('Contact Us');
        $this->Template->set('contact_us_url', $contact_us_url);
        $privacy_policy_url = Page::getPublicURLByName('Privacy Policy');
        $this->Template->set('privacy_policy_url', $privacy_policy_url);
        $terms_of_service_url = Page::getPublicURLByName('Terms of Service');
        $this->Template->set('terms_of_service_url', $terms_of_service_url);
        $copyright_infringement_url = Page::getPublicURLByName('Copyright Infringement');
        $this->Template->set('copyright_infringement_url', $copyright_infringement_url);


        $sitesList =& $this->Template->findChild('networkSites');
        if ($sitesList) {
        	$sites =& Site::getSites();
        	$this->Template->setChildDataSource('networkSites',$sites);
        }

        //$premiumVideoShow = Parameters::getPremiumVideoStatus();
        //if ($premiumVideoShow) $this->Template->set('premiumVideoShow', 1);

    }
}

class LayoutView extends View {
    var $IsAdminView;

	function LayoutView($TemplateFile) {
        parent :: View($TemplateFile);
    }

    function prepare() {
    	//parent::prepare();
    	$helper =& new LayoutViewHelper($this);
    	$helper->prepare();
    }

}

class NotFoundView extends LayoutView {

    function NotFoundView($TemplateFile) {
        parent :: LayoutView($TemplateFile);

    }
    function prepare() {
    	parent::prepare();
    	$docbase="http://".$_SERVER['HTTP_HOST'];
		$this->Template->set('docbase',$docbase);
		$this->Template->set('app_name',APP_NAME);
    }

}
?>
